﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class ActionRepository : BaseRepository<Action>, IActionRepository
    {
        public ActionRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 根据主键获取信息
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TAction ActionById(int aid)
        {
            try
            {
                return Context.TActions.Find(aid);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 添加操作按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool AddAction(TAction m)
        {
            //判断编码是不是唯一
            var aobj = Context.TActions.FirstOrDefault(x => x.ActionCode == m.ActionCode);
            if(aobj==null)
            {
                m.CreateUserId = m.NowUserId;
                m.CreateUserName = m.NowUserRealName;
                m.CreateDate = DateTime.Now;
                m.ModifyUserId = m.NowUserId;
                m.ModifyUserName = m.NowUserRealName;
                m.ModifyDate = DateTime.Now;
                Context.TActions.Add(m);
            }
            return Context.SaveChanges()>0;
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelAction(int aid)
        {
            try
            {
                //判断角色表是否为空
                var aobj = Context.TActions.Find(aid);
                if (aobj != null)
                {
                    //删除按钮
                    Context.TActions.Remove(aobj);
                    //查询角色菜单操作中该按钮并删除
                    var rma = Context.TRoleMenuActions.Where(x => x.ActionId == aid);
                    Context.TRoleMenuActions.RemoveRange(rma);
                    //查询菜单操作关系中该按钮并删除
                    var rw = Context.TMenuActions.Where(x => x.ActionId == aid);
                    Context.RemoveRange(rw);
                }
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取按钮列表
        /// </summary>
        /// <param name="aname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<TAction> GetActionList(string aname, int status, int pageIndex, int pageSize,out int totalCount)
        {
            try
            {
                var alist = Context.TActions.AsQueryable();

                if(!string.IsNullOrEmpty(aname))
                {
                    alist = alist.Where(x => x.ActionName.Contains(aname));
                }

                if(status<=1)
                {
                    alist = alist.Where(x => x.Status == status);
                }
                totalCount= alist.Count();
                return alist.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdAction(TAction m)
        {
            //判断编码是不是唯一
            var list = Context.TActions.AsNoTracking().FirstOrDefault(x => x.Id != m.Id && x.ActionCode == m.ActionCode);

            if (list != null)
            {
                return false;
            }
            m.ModifyUserId = m.NowUserId;
            m.ModifyUserName = m.NowUserRealName;
            m.ModifyDate = DateTime.Now;
            Context.TActions.Update(m);
  
            return Context.SaveChanges() > 0;
        }
    }
}
